package cn.sdhuijin.healthexam.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.EditText;
import cn.sdhuijin.healthexam.ExitApplication;
import cn.sdhuijin.healthexam.R;
import cn.sdhuijin.healthexam.control.AbstractControlActivity;
import cn.sdhuijin.healthexam.util.Constants;
import cn.sdhuijin.healthexam.util.Util;

/**
 * 新建人员档案
 * 
 * 修改完毕
 * 
 * @author houyuchun
 * 
 */
public class EditPersonDocFirstActivity extends AbstractControlActivity
		implements OnClickListener {

	// 创建日期ID
	private String createDate;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 去掉标题
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_edit_doc_first);
		ExitApplication.getInstance().addActivity(this);

		Intent intent = getIntent();
		Bundle bundle = intent.getBundleExtra("bundle");
		if (bundle != null) {
			createDate = bundle.getString("createDate");
		}
		setCurrentEditUserName(createDate);
		// 性别
		addRadioGroupView(R.id.rad_user_sex, "性别");
		// 常住类型
		addRadioGroupView(R.id.rad_hj_type, "常住类型");
		// 血型
		addRadioGroupView(R.id.rad_user_xuexing, "血型");
		// 婚姻状况
		addRadioGroupView(R.id.rad_marry_status, "婚姻状况");
		// 姓名
		addEditTextView(R.id.edt_user_name, "姓名");
		// 身份证号
		addEditTextView(R.id.edt_identify_num, "身份证号");
		// 出生日期
		addEditTextView(R.id.edt_user_birth, "出生日期");
		// 民族
		addEditTextView(R.id.edt_user_nation, "民族");
		// 联系电话
		addEditTextView(R.id.edt_user_phone, "联系电话");
		// 家庭地址
		addEditTextView(R.id.edt_home, "家庭地址");
		// 工作单位
		addEditTextView(R.id.edt_company, "工作单位");
		// 紧急联系人
		addEditTextView(R.id.edt_emergency_people, "紧急联系人");
		// 联系人电话
		addEditTextView(R.id.edt_emergency_phone, "联系人电话");
		addRadioGroupView(R.id.rad_rh_yinxing, "RH阴性");
		// Properties文件路径
		controlManager.setPropertiesPath(Constants.PROPERTIES_PATH + createDate
				+ ".txt");
		Log.i(EditPersonDocFirstActivity.class.getSimpleName(), "文件路径： "
				+ Constants.PROPERTIES_PATH + createDate + ".txt");
		controlManager.toControl();

		setEditTextListener(R.id.edt_user_birth);
		setButtonListener(this, R.id.btn_next, R.id.btn_back);
	}

	/**
	 * 按钮点击处理事件
	 */
	@Override
	public void onClick(View arg0) {
		Intent intent = new Intent();
		Bundle bundle = new Bundle();
		// 获取点击按钮ID
		int id = (Integer) arg0.getId();
		switch (id) {
		case R.id.btn_next:
			// 保存之前合法性验证，获取非法内容ID
			List<Integer> list = getInvalidateContentIds();
			// 如果存在不合法内容
			if (list.size() > 0) {
				// 非法内容背景颜色提示
				changeInvalidateContentBackColor(list);
				this.toastInformation(R.string.toast_input_error);
			} else {
				controlManager.toProperties();
				// 下一步按钮
				intent.setClass(this, EditPersonDocSecondActivity.class);
				bundle.putString("createDate", createDate);
				intent.putExtra("bundle", bundle);
				startActivity(intent);
				this.finish();
			}
			break;
		case R.id.btn_back:
			this.finish();
			break;
		default:
			break;
		}
	}

	private List<Integer> getInvalidateContentIds() {
		List<Integer> list = null;
		try {
			list = new ArrayList<Integer>();
			String validateStr = "";
			// 身份证号
			validateStr = ((EditText) this.findViewById(R.id.edt_identify_num))
					.getText().toString().trim();
			if (!"".equals(validateStr) && !Util.IDCardValidate(validateStr)) {
				list.add(R.id.edt_identify_num);
			}
			// 出生日期
			validateStr = ((EditText) this.findViewById(R.id.edt_user_birth))
					.getText().toString().trim();
			if (!"".equals(validateStr) && !Util.isDate(validateStr)) {
				list.add(R.id.edt_user_birth);
			}
			// 联系电话
			validateStr = ((EditText) this.findViewById(R.id.edt_user_phone))
					.getText().toString().trim();
			if (!"".equals(validateStr) && !Util.isMobileNum(validateStr)
					&& !Util.isTelphoneNum(validateStr)) {
				list.add(R.id.edt_user_phone);
			}
			// 紧急联系人电话
			validateStr = ((EditText) this
					.findViewById(R.id.edt_emergency_phone)).getText()
					.toString().trim();
			if (!"".equals(validateStr) && !Util.isMobileNum(validateStr)) {
				list.add(R.id.edt_emergency_phone);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	/**
	 * 改变录入信息不合法的控件背景颜色
	 * 
	 * @param list
	 *            控件ID列表
	 */
	private void changeInvalidateContentBackColor(List<Integer> list) {

		for (int i = 0; i < list.size(); i++) {
			if (R.id.edt_identify_num == list.get(i)) {
				((EditText) this.findViewById(R.id.edt_identify_num))
						.setBackgroundResource(R.drawable.diancha_test_pre);
			} else if (R.id.edt_user_birth == list.get(i)) {
				((EditText) this.findViewById(R.id.edt_user_birth))
						.setBackgroundResource(R.drawable.diancha_test_pre);
			} else if (R.id.edt_user_phone == list.get(i)) {
				((EditText) this.findViewById(R.id.edt_user_phone))
						.setBackgroundResource(R.drawable.diancha_test_pre);
			} else {
				((EditText) this.findViewById(R.id.edt_emergency_phone))
						.setBackgroundResource(R.drawable.diancha_test_pre);
			}
		}
	}
}
